function [IncAdvExpChallDem_Weighted_Polarization, IncAdvExpChallRep_Weighted_Polarization,  ExpChalltoIncDem_Weighted_Polarization, ExpChalltoIncRep_Weighted_Polarization] = getPolarization_Senate(Theta, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHSControls, UtilFuncParam, ChoiceProbForm, Dist_Index, Polarization_Factor)

Lambdas = Theta(1:2,:);
Betas = Theta(3:4,:);
Gamma = Theta(length(Theta),:);

if length(RHSControls(1,:)) >= 1
    RHSCoeff = Theta(5:4+length(RHSControls(1,:)),:);
    SingleIndexVal = CandTypeDem(:,1:2)*Betas(1:2,:) - CandTypeRep(:,1:2)*Betas(1:2,:) + RHSControls*RHSCoeff;
else
    SingleIndexVal = CandTypeDem(:,1:2)*Betas(1:2,:) - CandTypeRep(:,1:2)*Betas(1:2,:);
end


if length(RHSControls(1,:)) >= 1
    RHSCoeff = Theta(5:4+length(RHSControls(1,:)),:);

    SingleIndexValCounterFacIncDem = Betas(1,:) - CandTypeRep(:,1:2)*Betas(1:2,:) + RHSControls*RHSCoeff;
    SingleIndexValCounterFacIncRep = CandTypeDem(:,1:2)*Betas(1:2,:) - Betas(1,:) + RHSControls*RHSCoeff;
    
    SingleIndexValCounterFacExpChallDem = Betas(2,:) - CandTypeRep(:,1:2)*Betas(1:2,:) + RHSControls*RHSCoeff;
    SingleIndexValCounterFacExpChallRep = CandTypeDem(:,1:2)*Betas(1:2,:) - Betas(2,:) + RHSControls*RHSCoeff;
    
    
else
    
    SingleIndexValCounterFacIncDem = Betas(1,:) - CandTypeRep(:,1:2)*Betas(1:2,:);
    SingleIndexValCounterFacIncRep = CandTypeDem(:,1:2)*Betas(1:2,:) - Betas(1,:);

    
    SingleIndexValCounterFacExpChallDem = Betas(2,:) - CandTypeRep(:,1:2)*Betas(1:2,:);
    SingleIndexValCounterFacExpChallRep = CandTypeDem(:,1:2)*Betas(1:2,:) - Betas(2,:);
      
end


EstPosDem=(repmat(DemCandIdeal, 1, 2).*CandTypeDem)*(1-Lambdas) + (repmat(DemPartyIdeal, 1, 2).*CandTypeDem)*(Lambdas);
EstPosRep=(repmat(RepCandIdeal, 1, 2).*CandTypeRep)*(1-Lambdas) + (repmat(RepPartyIdeal, 1, 2).*CandTypeRep)*(Lambdas);
SingleIndexPolicy = -Gamma.*abs(EstPosDem-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep-VoterIdeal).^UtilFuncParam;

SingleIndex = SingleIndexPolicy + SingleIndexVal;


EstPosDem_Polarization=(repmat(DemCandIdeal, 1, 2).*CandTypeDem)*(1-Lambdas) + (repmat(Polarization_Factor*DemPartyIdeal, 1, 2).*CandTypeDem)*(Lambdas);
EstPosRep_Polarization=(repmat(RepCandIdeal, 1, 2).*CandTypeRep)*(1-Lambdas) + (repmat(Polarization_Factor*RepPartyIdeal, 1, 2).*CandTypeRep)*(Lambdas);


SingleIndexPolicy_Polarization = -Gamma.*abs(EstPosDem_Polarization-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep_Polarization-VoterIdeal).^UtilFuncParam;
SingleIndex_Polarization = SingleIndexPolicy_Polarization + SingleIndexVal;

EstPosDemCounterFacInc = DemCandIdeal*(1-Lambdas(1,:))+DemPartyIdeal*(Lambdas(1,:));
EstPosRepCounterFacInc = RepCandIdeal*(1-Lambdas(1,:))+RepPartyIdeal*(Lambdas(1,:));

EstPosDemCounterFacExpChall = DemCandIdeal*(1-Lambdas(2,:))+DemPartyIdeal*(Lambdas(2,:));
EstPosRepCounterFacExpChall = RepCandIdeal*(1-Lambdas(2,:))+RepPartyIdeal*(Lambdas(2,:));

EstPosDemCounterFacInc_Polarization = DemCandIdeal*(1-Lambdas(1,:))+Polarization_Factor*DemPartyIdeal*(Lambdas(1,:));
EstPosRepCounterFacInc_Polarization = RepCandIdeal*(1-Lambdas(1,:))+Polarization_Factor*RepPartyIdeal*(Lambdas(1,:));

EstPosDemCounterFacExpChall_Polarization = DemCandIdeal*(1-Lambdas(2,:))+Polarization_Factor*DemPartyIdeal*(Lambdas(2,:));
EstPosRepCounterFacExpChall_Polarization = RepCandIdeal*(1-Lambdas(2,:))+Polarization_Factor*RepPartyIdeal*(Lambdas(2,:));


SingleIndexPolicyCounterFacDemInc = -Gamma.*abs(EstPosDemCounterFacInc-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep-VoterIdeal).^UtilFuncParam;
SingleIndexPolicyCounterFacRepInc = -Gamma.*abs(EstPosDem-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRepCounterFacInc-VoterIdeal).^UtilFuncParam;

SingleIndexPolicyCounterFacDemExpChall = -Gamma.*abs(EstPosDemCounterFacExpChall-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep-VoterIdeal).^UtilFuncParam;
SingleIndexPolicyCounterFacRepExpChall = -Gamma.*abs(EstPosDem-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRepCounterFacExpChall-VoterIdeal).^UtilFuncParam;

SingleIndexPolicyCounterFacDemInc_Polarization = -Gamma.*abs(EstPosDemCounterFacInc_Polarization-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep-VoterIdeal).^UtilFuncParam;
SingleIndexPolicyCounterFacRepInc_Polarization = -Gamma.*abs(EstPosDem-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRepCounterFacInc_Polarization-VoterIdeal).^UtilFuncParam;

SingleIndexPolicyCounterFacDemExpChall_Polarization = -Gamma.*abs(EstPosDemCounterFacExpChall_Polarization-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRep-VoterIdeal).^UtilFuncParam;
SingleIndexPolicyCounterFacRepExpChall_Polarization = -Gamma.*abs(EstPosDem-VoterIdeal).^UtilFuncParam+Gamma.*abs(EstPosRepCounterFacExpChall_Polarization-VoterIdeal).^UtilFuncParam;

SingleIndexCounterFacDemInc = SingleIndexPolicyCounterFacDemInc + SingleIndexValCounterFacIncDem;
SingleIndexCounterFacRepInc = SingleIndexPolicyCounterFacRepInc + SingleIndexValCounterFacIncRep;

SingleIndexCounterFacDemExpChall = SingleIndexPolicyCounterFacDemExpChall + SingleIndexValCounterFacExpChallDem;
SingleIndexCounterFacRepExpChall = SingleIndexPolicyCounterFacRepExpChall + SingleIndexValCounterFacExpChallRep;


SingleIndexCounterFacDemInc_Polarization = SingleIndexPolicyCounterFacDemInc_Polarization + SingleIndexValCounterFacIncDem;
SingleIndexCounterFacRepInc_Polarization = SingleIndexPolicyCounterFacRepInc_Polarization + SingleIndexValCounterFacIncRep;

SingleIndexCounterFacDemExpChall_Polarization = SingleIndexPolicyCounterFacDemExpChall_Polarization + SingleIndexValCounterFacExpChallDem;
SingleIndexCounterFacRepExpChall_Polarization = SingleIndexPolicyCounterFacRepExpChall_Polarization + SingleIndexValCounterFacExpChallRep;


SingleIndexCounterFacDemInc_NoDir = SingleIndexPolicyCounterFacDemInc + SingleIndexVal;
SingleIndexCounterFacRepInc_NoDir = SingleIndexPolicyCounterFacRepInc + SingleIndexVal;

SingleIndexCounterFacDemExpChall_NoDir = SingleIndexPolicyCounterFacDemExpChall + SingleIndexVal;
SingleIndexCounterFacRepExpChall_NoDir = SingleIndexPolicyCounterFacRepExpChall + SingleIndexVal;

if ChoiceProbForm == 1

    PrVoteLeft = normcdf(SingleIndex, 0, 1);
    
    PrVoteLeft_Polarization = normcdf(SingleIndex_Polarization, 0, 1);
    
    PrVoteLeftCounterFacDemInc = normcdf(SingleIndexCounterFacDemInc, 0, 1);
    PrVoteLeftCounterFacRepInc = normcdf(SingleIndexCounterFacRepInc, 0, 1);
    
    PrVoteLeftCounterFacDemInc_Polarization = normcdf(SingleIndexCounterFacDemInc_Polarization, 0, 1);
    PrVoteLeftCounterFacRepInc_Polarization = normcdf(SingleIndexCounterFacRepInc_Polarization, 0, 1);
    
    PrVoteLeftCounterFacDemInc_NoDir = normcdf(SingleIndexCounterFacDemInc_NoDir, 0, 1);
    PrVoteLeftCounterFacRepInc_NoDir = normcdf(SingleIndexCounterFacRepInc_NoDir, 0, 1);

    PrVoteLeftCounterFacDemExpChall = normcdf(SingleIndexCounterFacDemExpChall, 0, 1);
    PrVoteLeftCounterFacRepExpChall = normcdf(SingleIndexCounterFacRepExpChall, 0, 1);
    
    PrVoteLeftCounterFacDemExpChall_Polarization = normcdf(SingleIndexCounterFacDemExpChall_Polarization, 0, 1);
    PrVoteLeftCounterFacRepExpChall_Polarization = normcdf(SingleIndexCounterFacRepExpChall_Polarization, 0, 1);
    
    PrVoteLeftCounterFacDemExpChall_NoDir = normcdf(SingleIndexCounterFacDemExpChall_NoDir, 0, 1);
    PrVoteLeftCounterFacRepExpChall_NoDir = normcdf(SingleIndexCounterFacRepExpChall_NoDir, 0, 1);
 
        
elseif ChoiceProbForm == 2
    
    PrVoteLeft = exp(SingleIndex)./(1+exp(SingleIndex)); 
    
    PrVoteLeft_Polarization = exp(SingleIndex_Polarization)./(1+exp(SingleIndex_Polarization)); 

    PrVoteLeftCounterFacDemInc = exp(SingleIndexCounterFacDemInc)./(1+exp(SingleIndexCounterFacDemInc));
    PrVoteLeftCounterFacRepInc = exp(SingleIndexCounterFacRepInc)./(1+exp(SingleIndexCounterFacRepInc));
  
    PrVoteLeftCounterFacDemInc_Polarization = exp(SingleIndexCounterFacDemInc_Polarization)./(1+exp(SingleIndexCounterFacDemInc_Polarization));
    PrVoteLeftCounterFacRepInc_Polarization = exp(SingleIndexCounterFacRepInc_Polarization)./(1+exp(SingleIndexCounterFacRepInc_Polarization));

    PrVoteLeftCounterFacDemInc_NoDir = exp(SingleIndexCounterFacDemInc_NoDir)./(1+exp(SingleIndexCounterFacDemInc_NoDir));
    PrVoteLeftCounterFacRepInc_NoDir = exp(SingleIndexCounterFacRepInc_NoDir)./(1+exp(SingleIndexCounterFacRepInc_NoDir)); 

    PrVoteLeftCounterFacDemExpChall = exp(SingleIndexCounterFacDemExpChall)./(1+exp(SingleIndexCounterFacDemExpChall));
    PrVoteLeftCounterFacRepExpChall = exp(SingleIndexCounterFacRepExpChall)./(1+exp(SingleIndexCounterFacRepExpChall));

    PrVoteLeftCounterFacDemExpChall_Polarization = exp(SingleIndexCounterFacDemExpChall_Polarization)./(1+exp(SingleIndexCounterFacDemExpChall_Polarization));
    PrVoteLeftCounterFacRepExpChall_Polarization = exp(SingleIndexCounterFacRepExpChall_Polarization)./(1+exp(SingleIndexCounterFacRepExpChall_Polarization));
       
    PrVoteLeftCounterFacDemExpChall_NoDir = exp(SingleIndexCounterFacDemExpChall_NoDir)./(1+exp(SingleIndexCounterFacDemExpChall_NoDir));
    PrVoteLeftCounterFacRepExpChall_NoDir = exp(SingleIndexCounterFacRepExpChall_NoDir)./(1+exp(SingleIndexCounterFacRepExpChall_NoDir)); 

end


ExpChalltoIncDem_Total = PrVoteLeftCounterFacDemInc(CandTypeDem(:,2)==1,:) - PrVoteLeft(CandTypeDem(:,2)==1,:);
ExpChalltoIncRep_Total = -(PrVoteLeftCounterFacRepInc(CandTypeRep(:,2)==1,:) - PrVoteLeft(CandTypeRep(:,2)==1,:));

ExpChalltoIncDem_Sig = PrVoteLeftCounterFacDemInc_NoDir(CandTypeDem(:,2)==1,:) - PrVoteLeft(CandTypeDem(:,2)==1,:);
ExpChalltoIncRep_Sig = -(PrVoteLeftCounterFacRepInc_NoDir(CandTypeRep(:,2)==1,:) - PrVoteLeft(CandTypeRep(:,2)==1,:));


IncAdvExpChallDem_Total = PrVoteLeft(CandTypeDem(:,1)==1,:)-PrVoteLeftCounterFacDemExpChall(CandTypeDem(:,1)==1,:);
IncAdvExpChallRep_Total = -(PrVoteLeft(CandTypeRep(:,1)==1,:)-PrVoteLeftCounterFacRepExpChall(CandTypeRep(:,1)==1,:));

IncAdvExpChallDem_Sig = PrVoteLeft(CandTypeDem(:,1)==1,:)-PrVoteLeftCounterFacDemExpChall_NoDir(CandTypeDem(:,1)==1,:);
IncAdvExpChallRep_Sig = -(PrVoteLeft(CandTypeRep(:,1)==1,:)-PrVoteLeftCounterFacRepExpChall_NoDir(CandTypeRep(:,1)==1,:));


%Weighting by District

ExpChalltoIncDem_Weighted_Holder = zeros(max(Dist_Index), 1);
ExpChalltoIncRep_Weighted_Holder = zeros(max(Dist_Index), 1);

ExpChalltoIncDem_Weighted_Holder_Polarization = zeros(max(Dist_Index), 1);
ExpChalltoIncRep_Weighted_Holder_Polarization = zeros(max(Dist_Index), 1);


ExpChalltoIncDem_Sig_Weighted_Holder = zeros(max(Dist_Index), 1);
ExpChalltoIncRep_Sig_Weighted_Holder = zeros(max(Dist_Index), 1);



IncAdvExpChallDem_Weighted_Holder = zeros(max(Dist_Index), 1);
IncAdvExpChallRep_Weighted_Holder = zeros(max(Dist_Index), 1);

IncAdvExpChallDem_Weighted_Holder_Polarization = zeros(max(Dist_Index), 1);
IncAdvExpChallRep_Weighted_Holder_Polarization = zeros(max(Dist_Index), 1);

IncAdvExpChallDem_Sig_Weighted_Holder = zeros(max(Dist_Index), 1);
IncAdvExpChallRep_Sig_Weighted_Holder = zeros(max(Dist_Index), 1);


for d = 1:max(Dist_Index)
    
    ExpChalltoIncDem_Weighted_Holder(d,:) = sum(PrVoteLeftCounterFacDemInc(Dist_Index==d & CandTypeDem(:,2)==1,:) - PrVoteLeft(Dist_Index==d & CandTypeDem(:,2)==1,:))/sum(Dist_Index==d & CandTypeDem(:,2) == 1);
    ExpChalltoIncRep_Weighted_Holder(d,:) =  sum(-(PrVoteLeftCounterFacRepInc(Dist_Index==d & CandTypeRep(:,2)==1,:) - PrVoteLeft(Dist_Index==d & CandTypeRep(:,2)==1,:)))/sum(Dist_Index==d & CandTypeRep(:,2) == 1);

    ExpChalltoIncDem_Weighted_Holder_Polarization(d,:) = sum(PrVoteLeftCounterFacDemInc_Polarization(Dist_Index==d & CandTypeDem(:,2)==1,:) - PrVoteLeft_Polarization(Dist_Index==d & CandTypeDem(:,2)==1,:))/sum(Dist_Index==d & CandTypeDem(:,2) == 1);
    ExpChalltoIncRep_Weighted_Holder_Polarization(d,:) =  sum(-(PrVoteLeftCounterFacRepInc_Polarization(Dist_Index==d & CandTypeRep(:,2)==1,:) - PrVoteLeft_Polarization(Dist_Index==d & CandTypeRep(:,2)==1,:)))/sum(Dist_Index==d & CandTypeRep(:,2) == 1);
    
    ExpChalltoIncDem_Sig_Weighted_Holder(d,:) = sum(PrVoteLeftCounterFacDemInc_NoDir(Dist_Index==d & CandTypeDem(:,2)==1,:) - PrVoteLeft(Dist_Index==d & CandTypeDem(:,2)==1,:))/sum(Dist_Index==d & CandTypeDem(:,2) == 1);
    ExpChalltoIncRep_Sig_Weighted_Holder(d,:) = sum(-(PrVoteLeftCounterFacRepInc_NoDir(Dist_Index==d & CandTypeRep(:,2)==1,:) - PrVoteLeft(Dist_Index==d & CandTypeRep(:,2)==1,:)))/sum(Dist_Index==d & CandTypeRep(:,2) == 1);

     
    IncAdvExpChallDem_Weighted_Holder(d,:) = sum(PrVoteLeft(Dist_Index==d & CandTypeDem(:,1) == 1,:)-PrVoteLeftCounterFacDemExpChall(Dist_Index==d & CandTypeDem(:,1) == 1,:))/sum(Dist_Index==d & CandTypeDem(:,1) == 1);
    IncAdvExpChallRep_Weighted_Holder(d,:) = sum(-(PrVoteLeft(Dist_Index==d & CandTypeRep(:,1) == 1,:)-PrVoteLeftCounterFacRepExpChall(Dist_Index==d & CandTypeRep(:,1) == 1,:)))/sum(Dist_Index==d & CandTypeRep(:,1) == 1);

    IncAdvExpChallDem_Weighted_Holder_Polarization(d,:) = sum(PrVoteLeft_Polarization(Dist_Index==d & CandTypeDem(:,1) == 1,:)-PrVoteLeftCounterFacDemExpChall_Polarization(Dist_Index==d & CandTypeDem(:,1) == 1,:))/sum(Dist_Index==d & CandTypeDem(:,1) == 1);
    IncAdvExpChallRep_Weighted_Holder_Polarization(d,:) = sum(-(PrVoteLeft_Polarization(Dist_Index==d & CandTypeRep(:,1) == 1,:)-PrVoteLeftCounterFacRepExpChall_Polarization(Dist_Index==d & CandTypeRep(:,1) == 1,:)))/sum(Dist_Index==d & CandTypeRep(:,1) == 1);
    
    
    IncAdvExpChallDem_Sig_Weighted_Holder(d,:) = sum(PrVoteLeft(Dist_Index==d & CandTypeDem(:,1)==1,:)-PrVoteLeftCounterFacDemExpChall_NoDir(Dist_Index==d & CandTypeDem(:,1)==1,:))/sum(Dist_Index==d & CandTypeDem(:,1) == 1);
    IncAdvExpChallRep_Sig_Weighted_Holder(d,:) = sum(-(PrVoteLeft(Dist_Index==d & CandTypeRep(:,1)==1,:)-PrVoteLeftCounterFacRepExpChall_NoDir(Dist_Index==d & CandTypeRep(:,1)==1,:)))/sum(Dist_Index==d & CandTypeRep(:,1) == 1);

end

ExpChalltoIncDem_Weighted = mean(ExpChalltoIncDem_Weighted_Holder(isnan(ExpChalltoIncDem_Weighted_Holder) == 0));
ExpChalltoIncRep_Weighted = mean(ExpChalltoIncRep_Weighted_Holder(isnan(ExpChalltoIncRep_Weighted_Holder) == 0));

ExpChalltoIncDem_Weighted_Polarization = mean(ExpChalltoIncDem_Weighted_Holder_Polarization(isnan(ExpChalltoIncDem_Weighted_Holder_Polarization) == 0));
ExpChalltoIncRep_Weighted_Polarization = mean(ExpChalltoIncRep_Weighted_Holder_Polarization(isnan(ExpChalltoIncRep_Weighted_Holder_Polarization) == 0));

ExpChalltoIncDem_Sig_Weighted = mean(ExpChalltoIncDem_Sig_Weighted_Holder(isnan(ExpChalltoIncDem_Sig_Weighted_Holder) == 0));
ExpChalltoIncRep_Sig_Weighted = mean(ExpChalltoIncRep_Sig_Weighted_Holder(isnan(ExpChalltoIncRep_Sig_Weighted_Holder) == 0));

IncAdvExpChallDem_Weighted = mean(IncAdvExpChallDem_Weighted_Holder(isnan(IncAdvExpChallDem_Weighted_Holder) == 0));
IncAdvExpChallRep_Weighted = mean(IncAdvExpChallRep_Weighted_Holder(isnan(IncAdvExpChallRep_Weighted_Holder) == 0));

IncAdvExpChallDem_Weighted_Polarization = mean(IncAdvExpChallDem_Weighted_Holder_Polarization(isnan(IncAdvExpChallDem_Weighted_Holder_Polarization) == 0));
IncAdvExpChallRep_Weighted_Polarization = mean(IncAdvExpChallRep_Weighted_Holder_Polarization(isnan(IncAdvExpChallRep_Weighted_Holder_Polarization) == 0));

IncAdvExpChallDem_Sig_Weighted = mean(IncAdvExpChallDem_Sig_Weighted_Holder(isnan(IncAdvExpChallDem_Sig_Weighted_Holder) == 0));
IncAdvExpChallRep_Sig_Weighted = mean(IncAdvExpChallRep_Sig_Weighted_Holder(isnan(IncAdvExpChallRep_Sig_Weighted_Holder) == 0));

SigDecomp_ExpChalltoIncDem = ExpChalltoIncDem_Sig_Weighted/ExpChalltoIncDem_Weighted;
SigDecomp_ExpChalltoIncRep = ExpChalltoIncRep_Sig_Weighted/ExpChalltoIncRep_Weighted;


SigDecomp_IncAdvExpChallDem = IncAdvExpChallDem_Sig_Weighted/IncAdvExpChallDem_Weighted;
SigDecomp_IncAdvExpChallRep = IncAdvExpChallRep_Sig_Weighted/IncAdvExpChallRep_Weighted;